﻿using LxBasic;

namespace LxCore
{
    public partial class ZoneCFDFields
    {
        protected void CheckCreate_Vel()
        {
            if (PhysicalModel.PhysicalType == PhysicalTypes.流体)
            {
                CheckCreator.Instance(ref SimpleGroup);
                SimpleGroup.CheckCreate(Zone);
            }
            else
            {
                SimpleGroup = null;
            }
        }
        protected void SetParameter_Vel()
        {
            if (Velocity == null) return;
            SimpleGroup.SetZone(LxSim.Sim.Definitions.SystemField.Density);
            var uvws = new FieldCombiner.Vector();
            if (PhysicalModel.BuoyancyType == BuoyancyTypes.Boussinesq模型)
            {
                if (!Density.IsParameter(out double _))
                {
                    LxLog.Warn("Boussinesq模型建议用于恒密度模型，请自行检查模型");
                }
                var value3D1Base = new BoussinesqSource(PhysicalModel.ReferenceValue.ReferenceTemperature, PhysicalModel.MaterialProperty.ThermalExpansionCoefficent, PhysicalModel.MaterialProperty.Density, PhysicalModel.ReferenceValue.Gravity, Zone);
                uvws.Add(value3D1Base);
            }
            var vis = PhysicalModel.MaterialProperty.Viscosity;
            IField<double> UVWgamma = vis;//TODO 湍流要改 GammaCommon == null ? vis : GammaCommon;
            Velocity.SetZone_CommonTerm(UVWgamma, PhysicalModel.MaterialProperty.Viscosity, uvws.Get(), ConstField.Vector0, PhysicalModel.InitialValue.InitialVelocity, PhysicalModel.SolveSetting.MomentumRelaxFactor);
            Velocity.SetZone_ConvTerm(PhysicalModel.SolveSetting.MomentumConvScheme);
            Velocity.SetZone_TransTerm(LxSim.Sim.Physics.IsTransient, ConstField.Double1, LxSim.Sim.Definitions.SystemField.Density);
            Pressure.SetZone(new ConstField.Double(0), PhysicalModel.SolveSetting.PressureRelaxFactor);

            foreach (var ti in Zone.Bounds)
            {
                var t = ZoneCond.BoundConds[ti.ID];
                switch (t.BoundType)
                {
                    case BoundTypes.壁面:
                        if (t.IsNoSlipingWall)
                        {
                            if (t.IsStaticWall)
                            {
                                SimpleGroup.SetBoundary_NoSlipStaticWall(ti);
                            }
                            else
                            {
                                SimpleGroup.SetBoundary_NoSlipMovingWall(t.Velocity, ti);
                            }
                        }
                        else
                        {
                            SimpleGroup.SetBoundary_SlipWall(ti);
                        }
                        break;
                    case BoundTypes.对称面:
                        SimpleGroup.SetBoundary_SlipWall(ti);
                        break;
                    case BoundTypes.速度入口:
                        if (t.IsNormalToBound)
                        {
                            SimpleGroup.SetBound_VelocityInlet(t.VelocityMagnitude, ti);
                        }
                        else
                        {
                            SimpleGroup.SetBound_VelocityInlet(t.Velocity, ti);
                        }
                        break;
                    case BoundTypes.压力出口:
                        if (t.IsNormalToBound)
                        {
                            SimpleGroup.SetBound_PressureInOut(t.Pressure, ti);
                        }
                        else
                        {
                            SimpleGroup.SetBound_PressureInOut( t.Pressure, t.Velocity, ti);
                        }
                        break;
                    default:
                        LxLog.Error("未知边界类型");
                        break;
                }
            }
        }
    }
}